package com.wpf.system.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wpf.system.common.response.ResultUtils;
import com.wpf.system.common.response.ResultVO;
import com.wpf.system.common.utils.MakeTree;
import com.wpf.system.entity.*;
import com.wpf.system.entity.dto.RoleMenuDTO;
import com.wpf.system.entity.vo.CascaderVO;
import com.wpf.system.entity.vo.RoleMenuVO;
import com.wpf.system.service.MenuService;
import com.wpf.system.service.RoleMenuService;
import com.wpf.system.service.RoleService;
import com.wpf.system.service.UserRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MenuService menuService;

    @Resource
    private UserRoleService userRoleService;

    @GetMapping("/{uid}")
    public List<UserRole> getUserRoleByUid(@PathVariable("uid") Long uid){
        return userRoleService.getUserRoleByUid(uid);
    }

    @GetMapping("/list/{uid}")
    public List<Role> getUserCanGiveRoleByUid(@PathVariable("uid") Long uid){
        return roleService.getUserCanGiveRoleByUid(uid);
    }

    @PreAuthorize("hasAuthority('sys:role:list')")
    @GetMapping({"/list/{page}/{pageSize}","/list/{page}/{pageSize}/{searchContent}"})
    public IPage<Role> getRoleList(@PathVariable("page") int page,
                                   @PathVariable("pageSize") int pageSize,
                                   @PathVariable(value = "searchContent",required=false) String searchContent
    ){ return roleService.getRoleList(page,pageSize,searchContent);}

    @PreAuthorize("hasAuthority('sys:role:add')")
    @PostMapping
    public ResultVO addRole(@RequestBody Role role){
        boolean flag = roleService.save(role);
        if (flag){
            return ResultUtils.success("添加角色成功！");
        }else{
            return ResultUtils.error("添加角色失败！");
        }
    }

    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PutMapping
    public ResultVO editRole(@RequestBody Role role){
        boolean flag = roleService.updateById(role);
        if (flag){
            return ResultUtils.success("修改角色成功！");
        }else{
            return ResultUtils.error("修改角色失败！");
        }
    }

    @PreAuthorize("hasAuthority('sys:role:edit')")
    @PutMapping("/status")
    public ResultVO editRoleStatus(@RequestBody Long id){
        boolean flag = Convert.toBool(roleService.editRoleStatusById(id));
        if (flag){
            return ResultUtils.success("修改角色状态成功！");
        }else{
            return ResultUtils.error("修改角色状态失败！");
        }
    }

    @PreAuthorize("hasAuthority('sys:role:delete')")
    @DeleteMapping("/{id}")
    public ResultVO deleteRole(@PathVariable("id") Long id){
        if(id==1L){
            return ResultUtils.error("禁止删除系统管理员角色！");
        }else{
            if (roleService.deleteRoleById(id)){
                return ResultUtils.success("删除角色成功！");
            }else{
                return ResultUtils.error("删除角色失败！");
            }
        }
    }

    @GetMapping("/menu/{uid}/{rid}")
    public RoleMenuVO getRoleMenu(@PathVariable Long uid, @PathVariable Long rid){
        return roleService.getRoleMenuListByRid(uid,rid);
    }

    @PreAuthorize("hasAuthority('sys:role:rolemenu')")
    @PostMapping("/menu")
    public ResultVO editRoleMenu(@RequestBody RoleMenuDTO roleMenuDTO){
        boolean flag = Convert.toBool(roleMenuService.saveRoleMenu(roleMenuDTO.getRid(), roleMenuDTO.getMidList()));
        if (flag){
            return ResultUtils.success("分配权限成功！");
        }else{
            return ResultUtils.error("分配权限失败！");
        }
    }

    @GetMapping("/menu/{rid}")
    public List<CascaderVO> getRoleMenuCascader(@PathVariable Long rid){
        return MakeTree.makeMenuCascader(menuService.getMenuByRid(rid),0L);
    }
}
